package com.itany.cloud.controller;

import com.itany.cloud.entity.User;
import com.itany.cloud.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/8/9 15:08
 * Version:1.0-SNAPSHOT
 * Description:
 */
@Api(tags = "用户服务功能")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    public static final String GLOBAL_URL = "http://localhost:8001/users";

    @ApiOperation(value = "查询所有用户信息",notes = "备注:该方法最后由xxx进行了修改")
    @GetMapping
    public ResponseResult findAll(){
        return restTemplate.getForObject(GLOBAL_URL,ResponseResult.class);
    }

    @ApiOperation("根据主键查询用户信息")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id",value = "用户id",dataType = "Integer")
    public ResponseResult findById(@PathVariable Integer id){
        return restTemplate.getForObject(GLOBAL_URL+"/{xxx}",ResponseResult.class,id);
    }

    @ApiOperation("添加用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",required = true),
            @ApiImplicitParam(name = "password",value = "密码",dataType = "String",required = true),
            @ApiImplicitParam(name = "phone",value = "电话",dataType = "String",required = true),
            @ApiImplicitParam(name = "address",value = "地址",dataType = "String",required = true)
    })
    @PostMapping
    public ResponseResult addUser(User user){
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("username",user.getUsername());
        params.add("password",user.getPassword());
        params.add("phone",user.getPhone());
        params.add("address",user.getAddress());
        return restTemplate.postForObject(GLOBAL_URL,params,ResponseResult.class);
    }

    @ApiOperation("修改指定用户信息")
    @PutMapping
    public ResponseResult modifyUser(User user){
        // put方法与delete方法均无返回值
        // 但是访问的请求需要其返回数据
        // 因此,此时可以考虑使用通用方法
//        restTemplate.put();
//        restTemplate.delete();
        String url = new StringBuffer()
                .append(GLOBAL_URL)
                .append("?id={id}")
                .append("&username={username}")
                .append("&password={password}")
                .append("&phone={phone}")
                .append("&address={address}")
                .toString();
        ResponseEntity<ResponseResult> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, null, ResponseResult.class, user.getId(), user.getUsername(), user.getPassword(), user.getPhone(), user.getAddress());
        return responseEntity.getBody();
    }

    @ApiIgnore
    @ApiImplicitParam(name = "id",value = "用户id",dataType = "Integer",required = true)
    @ApiOperation("删除指定用户信息")
    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable Integer id){
        ResponseEntity<ResponseResult> responseEntity = restTemplate.exchange(GLOBAL_URL + "/{id}", HttpMethod.DELETE, null, ResponseResult.class, id);
        return responseEntity.getBody();
    }
}
